#!/usr/bin/make -f
# debian/rules for wesnoth
# copyright 2007++ by Gerfried Fuchs <rhonda@debian.at>
# Licenced the same way as wesnoth itself

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# This has to be exported to make some magic below work.
export DH_OPTIONS

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

CFLAGS = -g
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
        CFLAGS += -O0
else
        CFLAGS += -O2
endif

CONFIGURE_SWITCHES = --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --disable-rpath --prefix=/usr --mandir=\$${prefix}/share/man --bindir=\$${prefix}/games --with-datadir-name=wesnoth --with-localedir=\$${prefix}/share/locale --with-fifodir=/var/run/wesnothd --datadir=\$${prefix}/share/games --enable-server --with-fribidi --enable-python-install CFLAGS="$(CFLAGS)" LDFLAGS="-Wl,-z,defs"


# calculate the version strings
MAJOR_VERSION = $(shell dpkg-parsechangelog | grep "^Version:" | cut -d" " -f2 | cut -d. -f1)
MINOR_VERSION = $(shell dpkg-parsechangelog | grep "^Version:" | cut -d" " -f2 | cut -d. -f2 | cut -d- -f1 | sed -e 's/[^0-9]//g')
UPSTREAM_VERSION =  $(shell dpkg-parsechangelog | grep "^Version:" | cut -d" " -f2 | rev | cut -d- -f2 | rev)

## if we are in a stable release we use this
STABLE_VERSION = $(MAJOR_VERSION).$(MINOR_VERSION)
NEXT_VERSION = $(MAJOR_VERSION).$(shell echo $$(( $(MINOR_VERSION) + 1 )) )

## for development releases, we use this so we won't have to change the
## control file
#STABLE_VERSION = $(UPSTREAM_VERSION)
#NEXT_VERSION = $(UPSTREAM_VERSION).~


include /usr/share/quilt/quilt.make

clean: unpatch
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp build-indep-stamp
	# Add here commands to clean up after the build process.
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f aclocal.m4 config.h.in Makefile.in configure m4/Makefile.in \
		po/Makefile.in doc/Makefile.in doc/man/Makefile.in \
		doc/manual/Makefile.in src/Makefile.in icons/Makefile.in \
		config/compile config/config.guess config/config.sub \
		config/install-sh config/missing config/depcomp \
		config/py-compile config.log
	find po -name "*.gmo" -exec rm -f {} \;
	find po -name "stamp-po" -exec rm -f {} \;
	rm -f config.sub config.guess
	dh_clean



#Architecture
build: build-arch build-indep

build-arch: build-arch-stamp
build-arch-stamp: patch
	./autogen.sh
	./configure $(CONFIGURE_SWITCHES)
	$(MAKE)
	touch $@


build-indep: build-indep-stamp
build-indep-stamp: patch
	# Add here commands to compile the indep part of the package.
	touch $@


install: install-indep install-arch

install-indep:
	dh_testdir
	dh_testroot
	dh_prep -i
	dh_installdirs -i

	# Add here commands to install the indep part of the package into
	# debian/<package>-doc.
	DESTDIR=$(CURDIR)/debian/tmp/ \
		$(MAKE) install-data-local \
			datadir=/usr/share \
			pkgdatadir=/usr/share/games/wesnoth

	cd $(CURDIR)/debian/tmp/usr/share/games/wesnoth/data/tools && chmod +x \
		extractbindings unit_tree/TeamColorizer \
		wesnoth/wescamp.py wesnoth/wmldata.py wesnoth/wmlparser.py \
		wmlindent wmlflip wmllint wmlscope wesnoth_addon_manager wmltest \
		hexometer/hexometer wmlxgettext


	DESTDIR=$(CURDIR)/debian/tmp/ \
		$(MAKE) -C po install \
			localedir=/usr/share/locale

	dh_install -i


install-arch:
	dh_testdir
	dh_testroot
	dh_prep -s
	dh_installdirs -s

	# Add here commands to install the arch part of the package into
	# debian/tmp.
	$(MAKE) DESTDIR=$(CURDIR)/debian/tmp install
	DESTDIR=$(CURDIR)/debian/tmp \
		$(MAKE) install-exec
	DESTDIR=$(CURDIR)/debian/tmp \
		$(MAKE) -C doc/man install

	dh_install -s


# Must not depend on anything. This is to be called by
# binary-arch/binary-indep
# in another 'make' thread.
binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs changelog
	dh_installdocs
	dh_installexamples
	dh_installmenu
	dh_lintian
	dh_pysupport
	dh_installinit
	dh_installman
	dh_strip --dbg-package=wesnoth-dbg
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol -- -Vwesnoth:Stable-Version=$(STABLE_VERSION) -Vwesnoth:Next-Version=$(NEXT_VERSION)
	dh_md5sums
	dh_builddeb


# Build architecture independant packages using the common target.
binary-indep: build-indep install-indep
	# workaround for dh_link (mis)behavior, see BTS#445550
	for i in ttf-dejavu/DejaVuSans.ttf; \
		do ln -s /usr/share/fonts/truetype/$$i \
	       	debian/wesnoth-data/usr/share/games/wesnoth/fonts/`basename $$i`; \
		done
	ln -s /usr/share/fonts/truetype/wqy/wqy-zenhei.ttc \
	       	debian/wesnoth-data/usr/share/games/wesnoth/fonts/wqy-zenhei.ttc

	for i in wesnoth wesnoth-all wesnoth-editor; do \
		install -p -d -m755 debian/$$i/usr/share/doc; \
		ln -s wesnoth-data debian/$$i/usr/share/doc/$$i; \
		done

	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

# Build architecture dependant packages using the common target.
binary-arch: build-arch install-arch
	for i in debian/wesnoth-core/usr/share/man/*/man6/wesnoth.6; do \
		ln -s wesnoth.6.gz `dirname $$i`/wesnoth-nolog.6.gz; \
		ln -s wesnoth.6.gz `dirname $$i`/wesnoth-smallgui.6.gz; \
		ln -s wesnoth.6.gz `dirname $$i`/wesnoth_editor.6.gz; \
		done
	ln -s wesnoth.6.gz debian/wesnoth-core/usr/share/man/man6/wesnoth-nolog.6.gz
	ln -s wesnoth.6.gz debian/wesnoth-core/usr/share/man/man6/wesnoth-smallgui.6.gz
	ln -s wesnoth.6.gz debian/wesnoth-core/usr/share/man/man6/wesnoth_editor.6.gz

	for i in wesnoth-core wesnoth-dbg; do \
		install -p -d -m755 debian/$$i/usr/share/doc; \
		ln -s wesnoth-data debian/$$i/usr/share/doc/$$i; \
		done

	$(MAKE) -f debian/rules DH_OPTIONS=-s binary-common


binary: binary-arch binary-indep
.PHONY: build clean binary-indep binary-arch binary install install-indep install-arch
